package com.hdjk.view.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.entity.GroupEntity;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by sxy on 2017/11/2.
 */

public class BuyGroupFundListAdapter extends BaseAdapter {
    private List<GroupEntity.PortfolioFund> mList;
    private Context mContext;
    private String mMoney = "0.00";

    public void setMoney(String money) {
        mMoney = money;
    }

    public BuyGroupFundListAdapter(Context context) {
        mContext = context;
    }

    public void setDataList(List<GroupEntity.PortfolioFund> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public GroupEntity.PortfolioFund getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_buy_group_fund_list, null);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_code = (TextView) convertView.findViewById(R.id.tv_code);
            holder.tv_scale = (TextView) convertView.findViewById(R.id.tv_scale);
            holder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        GroupEntity.PortfolioFund entity = mList.get(position);
        holder.tv_name.setText(entity.getFundName());
        holder.tv_code.setText(entity.getFundCode());
        holder.tv_scale.setText(entity.getProportion() + "%");
        if (TextUtils.isEmpty(mMoney))
            mMoney = "0.00";
        double l = Double.parseDouble(mMoney);
        BigDecimal b = new BigDecimal(l / 100 * Double.parseDouble(entity.getProportion()));
        holder.tv_money.setText("购买金额：" + b.setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "元");
        return convertView;
    }

    class Holder {
        TextView tv_name, tv_code, tv_scale, tv_money;
    }
}
